import { list } from "../../../..//api/shop.js"
const util = require('../../../../utils/util.js')
let app = getApp()
Component({
    lifetimes: {
        attached() {
            this.getLocation()
        },
    },
    data: {
        lng: 118.1,
        lat: 24.46,
        pageNum: 1,
        pageSize: 10,
        detailedList: [],
        shopCategoryName: '完美商家'
    },
    methods: {
        getLocation() {
            let longitude = 0
            let latitude = 0
            
            this.setData({
                lat: latitude,
                lng: longitude,
            })
            wx.getLocation({
                type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
                isHighAccuracy: true,
                success: res => {
                    wx.showLoading({
                        title: '定位中...',
                    })
                    this.setData({
                        lat: res.latitude,
                        lng: res.longitude,
                    })
                    this.getNearbyShops();
                },
                fail: err => {
                    this.getNearbyShops();
                }

            })
        },
        getNearbyShops() {
            console.log("getNearbyShops")
            this.loadinging = true;
            let param = {
                lat: this.data.lat,
                lng: this.data.lng,
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize,
                mchId: app.globalData.mchId,
            }
            list(param).then(res => {
                console.log(res, '2222');
                // res.data.forEach(item => {
                    this.data.detailedList = [...this.data.detailedList,...res.data]
                // })
                if (this.data.detailedList.length > 0) {
                    this.data.showDetail = true;
                }
                this.data.loadinging = false;
                this.setData({
                    detailedList: this.data.detailedList
                })
                setTimeout(() => {
                    wx.hideLoading()
                }, 1000)
            }).catch(err => {
                setTimeout(() => {
                    wx.hideLoading()
                }, 1000)
            })
        },
        onReachBottom() {
            this.data.pageNum += 1
            this.getLocation()
        },
        downRefresh() {
            wx.showLoading({
                title: '定位中...',
            })
            this.data.detailedList = []
            this.data.pageSize = 10
            this.data.pageNum = 1
            this.setData({
                detailedList: this.data.detailedList,
                pageSize: this.data.pageSize,
                pageNum: this.data.pageNum
            })
            this.getLocation()
        },
        navigate(e) {
            let item = e.currentTarget.dataset.item
            if (item.lat == null && item.lng == null) {
                this.data.lat = 24.46;
                this.data.lng = 118.1;
            } else {
                this.data.lat = item.lat;
                this.data.lng = item.lng;
            }
            let params = {   //使用微信内置地图查看标记点位置，并进行导航
                latitude: this.data.lat,//要去的纬度-地址
                longitude: this.data.lng,//要去的经度-地址
            }
            wx.openLocation(params)
        },
        clearData(){
            this.data.detailedList = []
        }
    }
})
